<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\facade\Session;
use app\index\model\Task\sign;
use app\index\model\User1;

class Tasks extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
          return $this->fetch('task');
    }
    //任务二
    public function invit(){
        return $this->fetch('invit');
    }
    public function sign(Request $request){
        Session::set('userid',1);
       $id= Session::get('userid');
    echo time();
        return $this->fetch('sign',['id'=>$id]);
    }
  //过了时间就能签到
    //当我们进入
    public function zidong(Request $request){
        $userid=$request->param('userid');
        $sign=Sign::where('userid',$userid)->find();
        $morging = strtotime(date('Ymd'))+24*3600-24*3600;
        $night=strtotime(date('Ymd'))+24*3600;
        //判断是否能签到了
     if(!empty($sign->times)){
       if($sign->times > $morging && $sign->times < $night ){
        if($sign->status==0){

            return json(['msg'=>0,'days'=>$sign->days]);
        }else{
            return json(['msg'=>1,'days'=>$sign->days]);
        }
      }else{
           $sign=Sign::get($userid);
           $sign->status=0;
           $sign->save();
           return json(['msg'=>0,'days'=>$sign->days]);
        }
    }else{
         return json(['msg'=>0,'days'=>$sign->days]);
        }
    }
    //点击签到处理的一些数据
    public function do_sign(Request $request){
        //用户从session里接过来的用户id
        $userid=$request->param('userid');
        //查询一下签到表
        $sign=Sign::where('userid',$userid)->find();
        //如果有时间签到是修改当前时间,如果没有时间让他添加时间
        if(empty($sign->times)){
            //没有时间用户还没签到过,点击签到让他添加
           $sign=new Sign();
            $sign->times=time();
            $sign->status=1;
            $sign->days=1;
            $sign->userid=$userid;
            $sign->save();
            $data=Sign::where('userid',$userid)->find();
            return json(['days'=>$data->days,'status'=>$data->status]);
        }else{
            //判断一下签到状态
            if($sign->status=='0'){
                //如果今天没有签到让他签到 更新签到的时间和状态
                $sign2=Sign::get($userid);
                $sign2->times=time();
                $sign2->status=1;
                //设置时间前天的0点时间
                $start = strtotime(date('Ymd'))+24*3600-24*3600-24*3600;
                //今天0点时间
                $night=strtotime(date('Ymd'))+24*3600;
                //查询昨天签到的时间是否在区间中 在就天数连续 不再天数从1开始
//                $time=Sign::whereTime('times', 'between', [$start, $night])
//                    ->select();

                if($sign->times > $start && $sign->times < $night ){
                    $sign2->days=$sign->days+1;
                }else{
                    $sign2->days=1;
                }
                $sign2->save();
                $data=Sign::where('userid',$userid)->find();
                return json(['days'=>$data->days,'status'=>$data->status]);

            }else{
                //如果状态是1 就直接发送数据到前台页面展示 出已经签到 还有显示当前的天数
                $data=Sign::where('userid',$userid)->find();
                return json(['days'=>$data->days,'status'=>$data->status]);
            }
        }
    }
    public function verificationCode()
    {
        $savePath = '../Public/qrcode/';
        $webPath = '/qrcode/';
        $qrData = 'http://c.tsz90.com/home/auth/login.html';
        $qrLevel = 'H';
        $qrSize = '8';
        $savePrefix = 'NickBai';
        $create=new User1();
        if($filename = $create->createQRcode($savePath, $qrData, $qrLevel, $qrSize, $savePrefix)){
            $pic = $webPath . $filename;
        }
//        echo "<img src='".$pic."'>";
        return json(['pic'=>$pic]);
    }

//    public function times(){
//        $start = strtotime(date('Ymd'))+24*3600-24*3600-24*3600;
//        $night=strtotime(date('Ymd'))+24*3600;
//       $time=Sign::
//            whereTime('times', 'between', [$start, $night])
//            ->select()->toArray();
//var_dump($time);
//        if(!empty($time['times'])){
//
//            echo 0;
//        }else{
//            echo 1;
//        }
//
//        $a= strtotime(date('Ymd'))+24*3600-24*3600;
//        $b=strtotime(date('Ymd'))+24*3600;
//
//        var_dump(date('Y-m-d H:i:s',$start));
//        var_dump(date('Y-m-d H:i:s',$night));
//        var_dump(date('Y-m-d H:i:s',$a));
//        var_dump(date('Y-m-d H:i:s',$b));
//
//    }


}
